package com.trajan.exercise.javaapi;

/*

   Created with IntelliJ IDEA.

   Ing. Tomáš Herich
   --------------------------- 
   19. 06. 2013
   1:15 PM

*/

public class StringExample {


    public static void main(String[] args) {

        StringExample methods = new StringExample();

        methods.substringTest();

    }

    public void substringTest() {

        String test = "test string";

        System.out.println(test.substring(3));
        System.out.println(test.substring(3, test.length()));


        String testString = "!";
        char testChar = '!';
        String sourceString = "This is a really nice test String for us to try on!";

        System.out.println("String \"" + testString + "\" count = " + countCharInString(sourceString, testString) + ", in string: " + sourceString);
        System.out.println("Char \'" + testChar + "\' count = " + countCharInString(sourceString, testChar) + ", in string: " + sourceString);




    }

    public int countCharInString(String sourceString, String searchedChar) {

        int count = 0;
        int lastIndex = 0;

        while (true) {

            int newIndex = sourceString.indexOf(searchedChar, lastIndex);

            if (newIndex == -1) {

                return count;

            } else {

                lastIndex = newIndex + 1;
                count++;

            }
        }
    }


    public int countCharInString(String sourceString, char searchedChar) {

        int count = 0;

        for (int i = 0; i < sourceString.length(); i++) {

            if (sourceString.charAt(i) == searchedChar) {
                count++;
            }

        }



        return count;
    }

}
